package com.ymatou.iapi.deviceid.testcase;

import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.deviceid.parameter.SaveBean;
import com.ymatou.iapi.deviceid.parameter.getFirstBean;
import com.ymatou.iapi.deviceid.service.getFirstCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DeviceidCallService;
import com.ymttest.database.mongo.DeviceidBean;
import com.ymttest.database.mongo.DeviceidWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*返回逻辑设备号和物理设备号中关联最老的一条激活时间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getFirst{
	private static getFirstBean getfirstBean;
	private static getFirstCall getfirstCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("返回逻辑设备号和物理设备号中关联最老的一条激活时间");
	}

	@Before
	public void caseUp() {
		getfirstBean=new getFirstBean();
		getfirstCall=new getFirstCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getfirstCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFirst_001() {
		Logger.start(true,"返回逻辑设备号和物理设备号中关联最老的一条激活时间-deviceID最早");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			saveBean1.setMachinename("iphoneX");
			DeviceidCallService.saveCall(saveBean1);
			
			SaveBean saveBean2 = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean2);
			
			getfirstBean.setDeviceId(saveBean1.getDeviceid());
			getfirstBean.setDid(saveBean2.getDid());
			
			getfirstCall.setData(getfirstBean);
			getfirstCall.callService();	

			Logger.verifyEquals("0", getfirstCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getfirstCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(getfirstBean.getDeviceId()+" or " + getfirstBean.getDid()+" exists", getfirstCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(getfirstBean.getDeviceId());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
					
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Date d = YMTDateUtil.getDate(newDate, YMTDateUtil.YMDTHMS_STRING);
				Logger.verifyEquals(YMTDateUtil.getFormatDateString(d), getfirstCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getfirstCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getfirstCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getfirstCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getfirstCall.getErrorCode(), "验证返回errorCode");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFirst_002() {
		Logger.start(true,"返回逻辑设备号和物理设备号中关联最老的一条激活时间-did最早");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean1);
			
			SaveBean saveBean2 = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean2);
			
			getfirstBean.setDeviceId(saveBean2.getDeviceid());
			getfirstBean.setDid(saveBean1.getDid());
			
			getfirstCall.setData(getfirstBean);
			getfirstCall.callService();	

			Logger.verifyEquals("0", getfirstCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getfirstCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(getfirstBean.getDeviceId()+" or " + getfirstBean.getDid()+" exists", getfirstCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(saveBean1.getDeviceid());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
					
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Date d = YMTDateUtil.getDate(newDate, YMTDateUtil.YMDTHMS_STRING);
				Logger.verifyEquals(YMTDateUtil.getFormatDateString(d), getfirstCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getfirstCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getfirstCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getfirstCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getfirstCall.getErrorCode(), "验证返回errorCode");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFirst_003() {
		Logger.start(true,"返回逻辑设备号和物理设备号中关联最老的一条激活时间-DeviceId空");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean1);
			
			getfirstBean.setDid(saveBean1.getDid());
			
			getfirstCall.setData(getfirstBean);
			getfirstCall.callService();	

			Logger.verifyEquals("100", getfirstCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getfirstCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("参数不能为空", getfirstCall.getMSG(), "验证返回Message");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFirst_004() {
		Logger.start(true,"返回逻辑设备号和物理设备号中关联最老的一条激活时间-DId空");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean1);
			
			getfirstBean.setDeviceId(saveBean1.getDeviceid());
			
			getfirstCall.setData(getfirstBean);
			getfirstCall.callService();	

			Logger.verifyEquals("100", getfirstCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getfirstCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals("参数不能为空", getfirstCall.getMSG(), "验证返回Message");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFirst_005() {
		Logger.start(true,"当did=00000000-0000-0000-0000-000000000000只查DeviceId-JIRACONN-2835");
		try {

			SaveBean saveBean1 = DeviceidCallService.setDefaultFullSaveBean();
			saveBean1.setDid("00000000-0000-0000-0000-000000000000");
			DeviceidCallService.saveCall(saveBean1);
			
			SaveBean saveBean2 = DeviceidCallService.setDefaultFullSaveBean();
			DeviceidCallService.saveCall(saveBean2);
			
			getfirstBean.setDeviceId(saveBean2.getDeviceid());
			getfirstBean.setDid(saveBean1.getDid());
			
			getfirstCall.setData(getfirstBean);
			getfirstCall.callService();	

			Logger.verifyEquals("0", getfirstCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getfirstCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(getfirstBean.getDeviceId()+" or " + getfirstBean.getDid()+" exists", getfirstCall.getMSG(), "验证返回Message");

			List<DeviceidBean> deviceidBeans = new DeviceidWapper().getByDeviceid(getfirstBean.getDeviceId());

			Logger.verifyEquals(1, deviceidBeans.size(), "验证Monggo库生成一条DeviceId记录。");

			if (deviceidBeans.size() == 1) {
				DeviceidBean bean = deviceidBeans.get(0);
					
				String date=bean.getActiveTime().toString().subSequence(7, 26).toString();
				int hour=Integer.parseInt(date.substring(11, 13))+8;
	
				String newDate = date.substring(0, 11)+hour+date.substring(13, 19);
				
				Date d = YMTDateUtil.getDate(newDate, YMTDateUtil.YMDTHMS_STRING);
				Logger.verifyEquals(YMTDateUtil.getFormatDateString(d), getfirstCall.getActiveTime(),
						"验证返回activeTime");
				Logger.verifyEquals(bean.getUserid(), Integer.parseInt(getfirstCall.getUserId()), "验证返回userId");
				Logger.verifyEquals(bean.getSignVerified(), Integer.parseInt(getfirstCall.getSignVerified()),
						"验证返回signVerified");
				Logger.verifyEquals("true", getfirstCall.getSuccess(), "验证返回success");
				Logger.verifyEquals("null", getfirstCall.getErrorCode(), "验证返回errorCode");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getFirst_006() {
		Logger.start(true,"当did=00000000-0000-0000-0000-000000000000只查DeviceId-JIRACONN-2835");
		try {

			getfirstBean.setDeviceId("510DEE11-8BCE-4C50-B012-D1BAB8AED30A");
			getfirstBean.setDid("15645646464");
			
			getfirstCall.setData(getfirstBean);
			getfirstCall.callService();	

			Logger.verifyEquals("0", getfirstCall.getCode(), "验证返回Code");
			Logger.verifyEquals("0", getfirstCall.getBCode(), "验证返回BCode");
			Logger.verifyEquals(getfirstBean.getDeviceId()+" or " + getfirstBean.getDid()+" exists", getfirstCall.getMSG(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}